package com.forlove.app.qselect.answer.presenter;

import android.text.TextUtils;

import com.forlove.app.ForloveApplication;
import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.manager.FriendManager;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.answer.entity.AnswerEntity;
import com.forlove.app.qselect.answer.service.ICircleGuideService;
import com.forlove.app.qselect.answer.service.impl.CircleGuideServiceImpl;
import com.forlove.app.qselect.answer.view.CircleGuideView;
import com.forlove.app.qselect.friend.entity.FriendEntity;
import com.forlove.app.qselect.friend.entity.FriendListEntity;
import com.forlove.app.qselect.login.entity.UserEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yjl on 2017/10/19.
 */

public class CircleGuidePresenter extends IBasePresenter<ICircleGuideService> {
    private CircleGuideView circleGuideView;
    private ICircleGuideService circleGuideService;

    public CircleGuidePresenter(CircleGuideView circleGuideView) {
        this.circleGuideView = circleGuideView;
        if (circleGuideService == null) {
            circleGuideService = new CircleGuideServiceImpl();
        }
        setService(circleGuideService);
    }


    public void loadQuestionData(UserEntity userEntity) {
        if (userEntity == null) {
            return;
        }
        Map<String, String> param = new HashMap<>();
        circleGuideService.loadData(ServerManager.ACTION_GET_SUBJECT, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "获取题目 ：" + result);
                AnswerEntity answerEntity = ParseHelper.getModel(result, "data", AnswerEntity.class);
                if (circleGuideView != null) {
                    circleGuideView.updateAnswerView(answerEntity);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    circleGuideView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    circleGuideView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }


    /**
     * 获取好友列表
     */
    public synchronized void loadFriendListData() {
        circleGuideService.loadData(ServerManager.ACTION_FRIEND_LIST, null, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "好友列表:" + result);
                List<FriendEntity> friendEntities = new ArrayList<>();
                friendEntities = ParseHelper.getModelList(result, "data.friends", FriendEntity.class);
                FriendListEntity friendListEntity = ParseHelper.getModel(result, "data", FriendListEntity.class);
                if (friendListEntity != null && friendListEntity.friendEntities.size() > 0) {
                    FriendManager.saveFriendList(ForloveApplication.getInstance(), result);
                }
                circleGuideView.updateFriendListView(friendEntities);
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    circleGuideView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    circleGuideView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }

}
